package fic.storefinder.client.content.composite;

import java.io.Serializable;
import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import fic.storefinder.client.content.ContentAbstract;
import fic.storefinder.client.content.ContentContainer;
import fic.storefinder.client.content.EditRobeDialog;
import fic.storefinder.client.content.ImageUploaderDialog;
import fic.storefinder.client.listener.UpdateDBListener;
import fic.storefinder.client.service.RobeService;
import fic.storefinder.client.service.RobeServiceAsync;
import fic.storefinder.shared.serializable.POI;
import fic.storefinder.shared.serializable.Robe;
import fic.storefinder.shared.utils.Constants;
import fic.storefinder.shared.utils.Utils;

import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class RobeListComposite extends ContentAbstract implements UpdateDBListener {
	private final RobeServiceAsync robeService = GWT.create(RobeService.class);
	private FlexTable flexTable;
	private POI poi;
	
	public RobeListComposite(final POI poi) {
		
		this.poi = poi;
		
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("", "");
		
		Label lblPoiName = new Label(poi.getPoiTitle());
		lblPoiName.setStyleName("poiName");
		lblPoiName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(lblPoiName);
		
		flexTable = new FlexTable();
		flexTable.setBorderWidth(1);
		verticalPanel.add(flexTable);
		flexTable.setSize("600px", "");
		
		Label lblDesc = new Label("Leírás");
		flexTable.setWidget(0, 0, lblDesc);
		
		Label lblPrice = new Label("Ar");
		flexTable.setWidget(0, 1, lblPrice);
		
		Label lblIsAvailable = new Label("Kölcsönözhető?");
		flexTable.setWidget(0, 2, lblIsAvailable);
		
		Label lblNewLabel = new Label("Műveletek");
		flexTable.setWidget(0, 3, lblNewLabel);
		flexTable.getFlexCellFormatter().setColSpan(0, 3, 2);
		flexTable.getCellFormatter().setVerticalAlignment(0, 3, HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		
		Button btnNewRobe = new Button("Új ruha felvétele");
		verticalPanel.add(btnNewRobe);
		btnNewRobe.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				Robe robe = new Robe();
				robe.setPoiId(poi.getPoiId());
				EditRobeDialog newRobeDialog = new EditRobeDialog(Constants.EDIT_TYPE_NEW, robe, RobeListComposite.this);
				newRobeDialog.center();
				
			}
		});
		
		Button btnBack = new Button("Vissza a boltlistához");
		verticalPanel.add(btnBack);
		btnBack.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				ContentContainer.getInstance().setPageContent(new StoreListComposite());
			}
		});
		
		
		this.listRobes();
		
	}
	
	
	private void listRobes() {
		robeService.getRobeListForPOI(poi, new AsyncCallback<ArrayList<Robe>>() {
			
			@Override
			public void onSuccess(ArrayList<Robe> result) {
				if (result == null) {
					return;
				}
				
				//remove prev rows from flextable:
				for (int i = 1; i < flexTable.getRowCount(); i++) {
					flexTable.removeRow(i);
				}
				
				int i = 1;
				
				for (final Robe robe : result) {
					
					Label lblDesc = new Label(robe.getDescription());
					flexTable.setWidget(i, 0, lblDesc);
					
					Label lblPrice = new Label(Utils.formatCurrency(robe.getPrice(), robe.getCurrency()));
					flexTable.setWidget(i, 1, lblPrice);
		
					Label lblAvailable = new Label(robe.getAvailable() == Constants.ROBE_AVAILABLE_YES ? "Igen" : "Nem");
					flexTable.setWidget(i, 2, lblAvailable);
					
					Button btnEdit = new Button("Szerkesztés");
					flexTable.setWidget(i, 3, btnEdit);
					btnEdit.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							EditRobeDialog newRobeDialog = new EditRobeDialog(Constants.EDIT_TYPE_UPDATE, robe, RobeListComposite.this);
							newRobeDialog.center();
						}
					});
					
					Button btnUploadImage = new Button("Kép feltöltése");
					flexTable.setWidget(i, 4, btnUploadImage);
					btnUploadImage.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							ImageUploaderDialog imgUploader = new ImageUploaderDialog(robe);
							imgUploader.center();
						}
					});
					
					i++;
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				RobeListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
			}
		});
	}


	@Override
	public void insert(Serializable tableObject) {
		if (tableObject instanceof Robe) {
			robeService.insertRobe((Robe) tableObject, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					RobeListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
				}

				@Override
				public void onSuccess(String result) {
					RobeListComposite.this.listRobes();
				}
			});
		}
		
	}


	@Override
	public void update(Serializable tableObject) {
		if (tableObject instanceof Robe) {
			robeService.updateRobe((Robe) tableObject, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					RobeListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
				}

				@Override
				public void onSuccess(String result) {
					RobeListComposite.this.listRobes();
				}
			});
		}
	}


	@Override
	public void delete(Serializable tableObject) {
		if (tableObject instanceof Robe) {
			robeService.deleteRobe((Robe) tableObject, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					RobeListComposite.this.showDialog("Szerver oldali hiba", caught.getClass().getName() + ": " +  caught.getMessage());
				}

				@Override
				public void onSuccess(String result) {
					RobeListComposite.this.listRobes();
				}
			});
		}
	}
}
